package com.smartview.jboss.spring.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.smartview.jboss.spring.dto.WeatherDTO;

@Repository
@Transactional
public class WeatherDao {
	// We need to use the entity manager factory bean provided by spring framework
	@PersistenceContext
	private EntityManager em;
	
	@Transactional(readOnly = true)
	public WeatherDTO getWeather(String zip) {
		// Here, I am going to the DB to grab the weather info
		// make it as an instance of weather,and then return it to you.
		return em.find(WeatherDTO.class, zip);
	}
	
	public void saveWeather(WeatherDTO weather) {
		// You give me the weather information, and I will save it to the DB
		em.persist(weather);
	}
	
	@Transactional(readOnly = true)
	public List<WeatherDTO> getWeatherList() {
		@SuppressWarnings("unchecked")
		List<WeatherDTO> list = em.createQuery("select w from WeatherDTO w").getResultList();
		return list;
	}
}
